import 'package:flutter/widgets.dart';
import 'package:safetymonitor/app/components/common/no_permissions_widget.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_cardx.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_search_enter.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_tree_picker.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/common/choice_bean.dart';
import 'package:safetymonitor/app/data/entities/home/knownledge/knowledge_base_info_entity.dart';
import 'package:safetymonitor/app/data/entities/home/knownledge/knowledge_base_type_tree_entity.dart';
import 'package:safetymonitor/generated/assets.dart';
import 'package:safetymonitor/router/app_pages.dart';

part 'ctrl.dart';

part 'iface.dart';

final class KnowledgeBasePage extends StatefulWidget {
  const KnowledgeBasePage({super.key});

  @override
  State<KnowledgeBasePage> createState() => _KnowledgeBasePageState();
}

final class _KnowledgeBasePageState extends State<KnowledgeBasePage> {
  final _refreshKey = GlobalKey<CustomEasyRefreshListState>();

  @override
  void initState() {
    _fetchTypes();
    super.initState();
  }

  @override
  void dispose() {
    _selectedType.value = _search = null;
    _types.value.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ListenableBuilder(
      listenable: isNotPermissions,
      builder: (_, __) {
        if (!isNotPermissions.value) {
          return const NoPermissionsWidget();
        }
        return Scaffold(
          appBar: AppBar(
            title: Text('知识库', style: TextStyle(fontSize: 16.w)),
            backgroundColor: Colors.white,
            centerTitle: true,
          ),
          backgroundColor: const Color(0xffF7FBFE),
          body: _buildBody(),
        );

      },
    );
  }

  Widget _buildBody() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        AppGaps.vGap10,
        Row(
          children: [
            Expanded(child: _buildSearch()),
            AppGaps.hGap10,
            Expanded(child: _buildFilter()),
          ],
        ).paddingSymmetric(horizontal: 10.w),
        AppGaps.vGap10,
        _buildList(),
      ],
    );
  }

  Widget _buildFilter() {
    return ValueListenableBuilder(
      valueListenable: _selectedType,
      builder: (_, val, __) {
        return CustomDropdownSelection(
          hintText: val == null ? '请选择类型' : null,
          text: val?.treeName,
          onCallBack: () => _showTypePicker(_refreshList),
        );
      },
    );
  }

  Widget _buildSearch() {
    return CustomSearchEnter(
      hintText: '请输入知识标题',
      onSubmitted: (p0) {
        _search = p0;
        _refreshList();
      },
    );
  }

  Widget _buildList() {
    return Expanded(
      child: CustomEasyRefreshList(
        key: _refreshKey,
        dataLoad: _fecthList,
        padding: EdgeInsets.symmetric(horizontal: 10.w),
        itemBuilder: (item, idx) {
          return CustomCardX(
            onTap: () {
              // Navigator.of(context).pushNamed(
              //   Routes.knowledgeBaseDetail,
              //   arguments: item,
              // );
              Get.toNamed(Routes.knowledgeBaseDetail, arguments: item);
            },
            child: Row(
              children: [
                SvgPicture.asset(
                  Assets.svgBlueBgBook,
                  height: 47.w,
                  width: 47.w,
                ),
                AppGaps.hGap10,
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        item.fileName ?? '未知文件名',
                        style: TextStyle(
                          color: const Color(0xff3D4769),
                          fontSize: 15.w,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      AppGaps.vGap2,
                      Text(
                        item.createTime?.millTS2Str() ?? '未知日期',
                        style: TextStyle(
                          color: Colors.grey,
                          fontSize: 14.w,
                        ),
                      ),
                    ],
                  ),
                ),
                // const Spacer(),
                Icon(Icons.arrow_forward, size: 20.w),
                AppGaps.hGap6,
              ],
            ),
          );
        },
      ),
    );
  }

  void _refreshList() {
    _refreshKey.currentState?.onRefresh();
  }
}
